package ae.pcfc.cldos.online.web.services.security.provider;

import java.security.Principal;
import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.web.tomcat.security.login.WebAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import ae.pcfc.cldos.online.web.services.security.MyTokenBasedRememberMeService;
import ae.pcfc.cldos.online.web.services.security.SecurityService;
import ae.pcfc.cldos.online.web.services.security.beans.LoginBean;

/**
 * Custom security service
 * 
 * @author arosa
 *
 */
@Service
public class SecurityServiceImpl implements SecurityService {
	
	@Resource(name="tokenBasedRememberMeServices")
	private MyTokenBasedRememberMeService rememberMeServices;
	
	@Autowired
	AuthenticationManager authenticationManager;
	
	private final GrantedAuthority __COMPANY_ADMIN = new GrantedAuthorityImpl("ROLE_COMPANY_ADMIN");
	private final GrantedAuthority __COMPANY_EMPLOYEE = new GrantedAuthorityImpl("ROLE_COMPANY_EMPLOYEE");
	private final GrantedAuthority __ADMIN_GENERAL = new GrantedAuthorityImpl("ROLE_ADMIN_GENERAL");
	private final GrantedAuthority __ADMIN_IT = new GrantedAuthorityImpl("ROLE_ADMIN_IT");
	private final GrantedAuthority __DAFAULT_USER = new GrantedAuthorityImpl("ROLE_DEFAULT_USER");
	
	
	public boolean login(HttpServletRequest request, HttpServletResponse response, LoginBean loginBean){
		
		boolean loginSuccess = false;
		
		String username = loginBean.getLogin();
		String password = loginBean.getPassword();
		boolean isRememberMe = loginBean.isRememberMe();
		
		if(username!=null && !username.isEmpty() && password!=null && !password.isEmpty()){
			WebAuthentication pwl = new WebAuthentication();
			loginSuccess = pwl.login(username, password);
			if(loginSuccess && isRememberMe){
				Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
				rememberMeServices.onLoginSuccess(request, response, authentication);
			}
		}
		
		return loginSuccess;
	}
	
	private Authentication getAuthentication(){
		return SecurityContextHolder.getContext().getAuthentication();
	}
	
	public boolean isAuthenticated(){
		Authentication authentication = getAuthentication();
		return authentication!=null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();
	}
	
	public boolean isAuthenticatedByRequest(HttpServletRequest request){
		Principal principal = request.getUserPrincipal();
		return principal!=null;
	}
	
	public Collection<GrantedAuthority> getCurrentLoginedUserRole(){
		Authentication authentication = getAuthentication();
		return authentication.getAuthorities();
	}
	
	public boolean isCompanyAdmin(){
		return getCurrentLoginedUserRole().contains(__COMPANY_ADMIN);
	}
	
	public boolean isCompanyEmployee(){
		return getCurrentLoginedUserRole().contains(__COMPANY_EMPLOYEE);
	}
	
	public boolean isGeneralAdmin(){
		return getCurrentLoginedUserRole().contains(__ADMIN_GENERAL);
	}
	
	public boolean isDefaultUser(){
		return getCurrentLoginedUserRole().contains(__DAFAULT_USER);
	}
	
	public boolean isITAdmin(){
		return getCurrentLoginedUserRole().contains(__ADMIN_IT);
	}
	
	public String getLoginedUsername(){
		Authentication authentication = getAuthentication();
		return authentication.getName();
	}
}
